Skip to content

Initial matcher/validator support #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 28, 2022
Merged

Conversation

timvermeulen
Copy link

Adds MatchingCollectionConsumer and MatchingCollectionSearcher as a matching/validating analogues of CollectionConsumer and CollectionSearcher:

public protocol MatchingCollectionConsumer: CollectionConsumer {
  associatedtype Value

  func matchingConsuming(
    _ consumed: Consumed,
    in range: Range<Consumed.Index>
  ) -> (Value, Consumed.Index)?
}

public protocol MatchingCollectionSearcher: CollectionSearcher {
  associatedtype Value

  func matchingSearch(
    _ searched: Searched,
    _ state: inout State
  ) -> (Value, Range<Searched.Index>)?
}

...as well as matching equivalents of the other consumer/searcher protocols.

Also adds a few algorithms that work with matches:

  • firstMatch/lastMatch
  • matches(of:)
  • replacing(_:with:) using a closure:
let regex = Regex(OneOrMore(.digit).capture { Int($0)! })
let str = "foo 160 bar 99 baz"
print(str.replacing(regex, with: { match, _ in String(match.1, radix: 8) }))
// foo 240 bar 143 baz

}

func testMatchReplace() {
let regex = Regex(OneOrMore(.digit).capture { Int($0)! })
Copy link
Contributor

@rxwei rxwei Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we get additional examples of find-and-replace with a regex with multiple captures, both with the regex literal and with the result builder syntax. That will help inform our decisions around Swift tuples vs nominal tuples.

@timvermeulen timvermeulen marked this pull request as ready for review January 27, 2022 19:42
@timvermeulen
Copy link
Author

@swift-ci Please test Linux

@timvermeulen timvermeulen requested a review from milseman January 27, 2022 23:21
Copy link
Member

@milseman milseman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -9,56 +9,54 @@
//
//===----------------------------------------------------------------------===//

import _MatchingEngine
public struct RegexConsumer<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me for now.

Outside scope of this PR, we might make RegexProtocol itself a consumer/searcher/matcher. But, I think it's best to establish parent-child relationships after we have the fundamental model in place. The important thing is getting this code and conformances in.

BTW, why does this need to be public?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some consumer/searcher algorithms return a type generic over the searcher, so the regex overloads return a type generic over RegexConsumer. So same story as PredicateConsumer at the moment.

@timvermeulen timvermeulen merged commit e719d6d into swiftlang:main Jan 28, 2022
@timvermeulen timvermeulen deleted the matchers branch January 28, 2022 20:50
milseman pushed a commit to milseman/swift-experimental-string-processing that referenced this pull request Feb 2, 2022
* Initial matchers support

* Conform `RegexConsumer` to the matcher protocols

* Add `firstMatch`/`lastMatch` and match find/replace

* Add `_MatchResult` and allow `RegexProtocol` in algorithms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants